/****************************************************************************/
/*This program runs regressions                                             */
/****************************************************************************/

#delimit;
clear;
set mem 1200m;
set matsize 800;
set more off;
capture log close;
set trace off;

/****************************************************************************/
/* DEFINE THE PROJECT FOLDER                                                */
/****************************************************************************/

*local dropboxpath C:\Users\srohlin\Dropbox\Tornadoes;
local pathsmr  C:\Research

/****************************************************************************/
/*DEFINE PATH TO FOLDERS                                                    */
/****************************************************************************/

*local Torpath  `pathsmr'\Tornadoes;
local Torpath  `pathsmr'\Tornadoes\0515Miles_CountyM;

/****************************************************************************/
/*Open the data                                                             */
/****************************************************************************/

use `Torpath'\Data\business_tornado_data_final_sizev2.dta, clear;

/****************************************************************************/
/*Create LHS variables to be used in the regressions                        */
/****************************************************************************/

/*************************/
/*Create ln LHS variables*/
/*************************/

global outcomestubs "firm_lt3 emp_lt3 sales_lt3 firm_gt3lt7 emp_gt3lt7 sales_gt3lt7 firm_gt7 emp_gt7 sales_gt7" ;
global industrystubs "all rs" ;

foreach i of global outcomestubs { ;
foreach j of global industrystubs { ;

gen ln_`i'_`j' = ln(`i'_`j' +1);

};
};

save `Torpath'\Temp\temp.dta, replace;

/***************************************************************************/
/* Create a loop that loops through the damage levels                      */
/***************************************************************************/

local k = 1;
while `k' <= 4{;

if `k' == 1 {;
local damvar alltreat;
};

if `k' == 2 {;
local damif g1;
local damvar g1treat;
};

if `k' == 3 {;
local damif g2;
local damvar g2treat;
}; 

if `k' == 4 {;
local damif g3;
local damvar g3treat;
}; 

/****************************************************************************/
/*Create RHS variables to be used in the regressions                        */
/****************************************************************************/

use `Torpath'\Temp\temp.dta, clear;

if `k' > 1 {;
*replace treated = 0 if dam_weight_`damif' == 0; 
drop if dam_weight_`damif' == 0; 
};

*gen ydate = tor_year;
gen ydate = tornado_year_temp;

tabulate year, generate(yeardum);

gen after = (year > ydate);
gen after_t = (after*treated);
gen ia_after = (after*individual_assistance);
gen ia_after_t = (after*individual_assistance*treated);
*gen pdd_after = (after*pdd);
*gen pdd_after_t = (after*pdd*treated);

gen after_2 = (year >= ydate);
gen after_t_2 = (after_2*treated);

gen balanced_dd = 0;
replace balanced_dd = 1 if year >= ydate-3 & year <= ydate+3;

gen balanced_dd_2 = 0;
replace balanced_dd_2 = 1 if year >= ydate-4 & year <= ydate+4;

gen ydate_m4p = ydate - 4;
gen ydate_m3 = ydate - 3;
gen ydate_m2 = ydate - 2;
gen ydate_m1 = ydate - 1;
gen ydate_p0 = ydate;
gen ydate_p1 = ydate + 1;
gen ydate_p2 = ydate + 2;
gen ydate_p3 = ydate + 3;
gen ydate_p4 = ydate + 4;
gen ydate_p5 = ydate + 5;
gen ydate_p6p = ydate + 6;

gen ysm4p = (year <= ydate_m4p);
gen ysm4 = (year == ydate_m4p);
gen ysm3 = (year == ydate_m3);
gen ysm2 = (year == ydate_m2);
gen ysm1 = (year == ydate_m1);
gen ysp0 = (year == ydate_p0);
gen ysp1 = (year == ydate_p1);
gen ysp2 = (year == ydate_p2);
gen ysp3 = (year == ydate_p3);
gen ysp4 = (year == ydate_p4);
gen ysp5 = (year == ydate_p5);
gen ysp6p = (year >= ydate_p6p);

gen pre = 0;
replace pre = 1 if ysm2 == 1 | ysm3 == 1 | ysm4 == 1;

gen post = 0;
replace post = 1 if ysp0 == 1 | ysp1 == 1 | ysp2 == 1 | ysp3 == 1 | ysp4 == 1;

gen pre_2 = 0;
replace pre_2 = 1 if ysm1 == 1 | ysm3 == 1 | ysm4 == 1;
gen pre_3 = 0;
replace pre_3 = 1 if ysm1 == 1 | ysm2 == 1 | ysm4 == 1;
gen pre_4 = 0;
replace pre_4 = 1 if ysm1 == 1 | ysm2 == 1 | ysm3 == 1;
gen pre_5 = 0;
replace pre_5 = 1 if ysm1 == 1 | ysm2 == 1 | ysm3 == 1 | ysp4 == 1;
/*post_2 drops event year 0*/

gen post_2 = 0;
replace post_2 = 1 if ysp1 == 1 | ysp2 == 1 | ysp3 == 1 | ysp4 == 1;

local vlist "pre pre_2 pre_3 pre_4 pre_5 post post_2";
foreach n of local vlist{;
gen `n'_t=`n'*treated;
gen `n'_ia=`n'*individual_assistance;
gen `n'_ia_t=`n'*individual_assistance*treated;
};

drop ydate_*;

local eventyear "ysm4p ysm4 ysm3 ysm2 ysm1 ysp0 ysp1 ysp2 ysp3 ysp4 ysp5 ysp6p";
foreach w of local eventyear{;
gen `w'_t= (`w'*treated);
gen  ia_`w'= (`w'*individual_assistance);
gen ia_`w'_t= (`w'*individual_assistance*treated);
};


save `Torpath'\Temp\temp_program_sizev2_`damvar'.dta, replace;
clear;
local k = `k' + 1;
};
